import * as types from './types'

export const state = {
  loans: [],
  loading: false,
  pageSize: 10,
  total: 0
}

export const getters = {
  loans: state => state.loans,
  loading: state => state.loading,
  pageSize: state => state.pageSize,
  total: state => state.total
}

export const actions = {
  getLoans({
    commit
  }, payload) {
    commit(types.GET_LOANS, payload)
  },

  checkLoan({
    commit
  }, payload) {
    commit(types.CHECK_LOAN, payload)
  },

  query({
    commit
  }, payload) {
    commit(types.QUERY, payload)
  },
}

export const mutations = {
  /**
   * 获取标的审核列表
   * 
   * @param {any} state 
   * @param {any} {
   *     vm
   *   } 
   */
  [types.GET_LOANS](state, {
    vm,
    page
  }) {
    state.loading = true

    const params = {
      start: 1,
      limit: state.pageSize
    }

    if (page) {
      params.start = page
    }

    vm.$ajax.get('/showLoans', {
        params: params
      })
      .then(res => {
        let data = res.data
        if (data != null && data.status == vm.$appConfig.success) {
          state.loans = data.list
          state.total = data.total
          state.pageSize = data.pageSize
        } else {
          state.loans = []
        }

        console.log(res.data)
        state.loading = false
      })
      .catch(error => {
        state.loans = []
        state.loading = false

        vm.$notify.error({
          title: '失败',
          message: '获取标的审核列表失败'
        })
        console.err(error)
      })
  },
  /**
   * 审核标的
   * 
   * @param {any} state 
   * @param {any} {
   *     vm,
   *     params,
   *     cb
   *   } 
   */
  [types.CHECK_LOAN](state, {
    vm,
    params,
    cb
  }) {
    vm.$ajax.post('/check/checkLoan', vm.$postParams(params))
      .then(res => {
        vm.$notify({
          title: '成功',
          message: '审核成功',
          type: 'success'
        })
        if (cb) {
          cb()
        }
      })
      .catch(error => {
        vm.$notify.error({
          title: '错误',
          message: '发生了一个错误'
        })
        console.error(error)
      })
  },
  /**
   * 条件查询标的
   * 
   * @param {any} state 
   * @param {any} {
   *     vm
   *   } 
   */
  [types.QUERY](state, {
    vm
  }) {
    vm.$notify({
      title: '提示',
      message: '方法尚未实现',
      type: 'warning'
    })
  },
}